package com.yy.wga.goods.service.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yy.wga.mp.common.interceptor.RequestUserHolder;
import com.yy.wga.mp.common.utils.mapping.BeansUtils;
import com.yy.wga.mp.module.goods.entity.pojo.GoodsUpGradeDO;
import com.yy.wga.mp.module.goods.manager.GoodsUpGradeManager;
import com.yy.wga.mp.module.goods.request.BuyGoodsCreateOrderRequest;
import com.yy.wga.mp.module.goods.request.GoodsUpGradeGetRequest;
import com.yy.wga.mp.module.goods.request.GoodsUpGradeInfoRequest;
import com.yy.wga.mp.module.goods.request.QueryGoodsPayResultRequest;
import com.yy.wga.mp.module.goods.response.BuyGoodsCreateOrderResponse;
import com.yy.wga.mp.module.goods.response.GoodsDetailResponse;
import com.yy.wga.mp.module.goods.response.GoodsUpGradeInfoResponse;
import com.yy.wga.mp.module.goods.response.QueryGoodsPayResultResponse;
import com.yy.wga.mp.module.goods.service.GoodsUpGradeService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 商品大礼包管理
 *
 * @author guochao
 * @since 2024-10-25 21:46:26
 */
@RestController
@RequestMapping("api/goods/upGrade")
public class GoodsUpGradeController {
    /**
     * 服务对象
     */
    @Resource
    private GoodsUpGradeService goodsUpGradeService;
    @Resource
    private GoodsUpGradeManager goodsUpGradeManager;

    /**
     * 商品列表
     *
     * @return
     */
    @PostMapping("list")
    public Result<List<GoodsUpGradeInfoResponse>> list(@RequestBody @Valid GoodsUpGradeInfoRequest request) {
        List<GoodsUpGradeInfoResponse> responseList = goodsUpGradeService.listGoods(request);
        return Result.success(responseList);
    }

    /**
     * 获取商品详情
     *
     * @param request
     * @return
     */
    @PostMapping("get")
    public Result<GoodsDetailResponse> get(@RequestBody @Valid GoodsUpGradeGetRequest request) {
        GoodsUpGradeDO goodsUpGrade = goodsUpGradeService.getOne(new LambdaQueryWrapper<GoodsUpGradeDO>()
                .eq(GoodsUpGradeDO::getAppKey, RequestUserHolder.getBaseRequest().getAppKey())
                .eq(GoodsUpGradeDO::getId, request.getId()));
        if (goodsUpGrade == null) {
            return Result.fail("商品不存在");
        }
        return Result.success(BeansUtils.map(goodsUpGrade, GoodsDetailResponse.class));
    }

    /**
     * 商品购买 - 1.预订单
     *
     * @param request
     * @return
     */
    @PostMapping("createOrder")
    public Result<BuyGoodsCreateOrderResponse> createOrder(@RequestBody @Valid BuyGoodsCreateOrderRequest request) {
        BuyGoodsCreateOrderResponse response = goodsUpGradeManager.createOrder(request);
        return Result.success(response);
    }

    /**
     * 查询支付结果
     *
     * @param request
     * @return
     */
    @PostMapping(value = "/queryPayResult", produces = "application/json")
    public Result<QueryGoodsPayResultResponse> queryPayResult(@RequestBody @Valid QueryGoodsPayResultRequest request) {
        return Result.success(goodsUpGradeManager.queryPayResult(request));
    }
}
